package com.goods.business.service.imp;


import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryTreeMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {


    @Autowired
    private ProductCategoryTreeMapper productCategoryTreeMapper;

    //1.1类别列表展示
    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum, Integer pageSize) {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getProductCategoryTreeNodeVOS();
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        List<ProductCategoryTreeNodeVO> page = ListPageUtils.page(build, pageSize, pageNum);
        PageInfo<ProductCategoryTreeNodeVO> pageInfo = new PageInfo<>(build);
        return new PageVO<>(pageInfo.getTotal(), page);
    }

    //1.2.1物资分类父级列表
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getProductCategoryTreeNodeVOS();
        List<ProductCategoryTreeNodeVO> products = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        return products;
    }

    //1.2.2添加分类
    @Override
    public void addParentCategoryTree(ProductCategory productCategory) {
        //创建时间
        productCategory.setCreateTime(new Date());
        //修改时间
        productCategory.setModifiedTime(new Date());
        productCategoryTreeMapper.insert(productCategory);

    }

    //1.3.1回显编辑分类
    @Override
    public ProductCategoryTreeNodeVO editParentCategoryTree(Long id) {
        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        ProductCategory productCategory1 = productCategoryTreeMapper.selectOne(productCategory);
        BeanUtils.copyProperties(productCategory1, productCategoryTreeNodeVO);

        return productCategoryTreeNodeVO;
    }

    //1.3.2修改分类
    @Override
    public void updateParentCategoryTree(Long id, ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {
        ProductCategory productCategory1 = new ProductCategory();
        BeanUtils.copyProperties(productCategoryTreeNodeVO, productCategory1);
        productCategory1.setId(id);
        productCategoryTreeMapper.updateByPrimaryKey(productCategory1);
    }

    //1.4 删除分类
    @Override
    public boolean deleteParentCategoryTree(Long id) {
        ProductCategory productCategory = new ProductCategory();
        //Pid 父分类的  id
        productCategory.setPid(id);
        //查询分类是否有子分类，有子分类的不能删
        ProductCategory productCategory1 = productCategoryTreeMapper.selectOne(productCategory);
        //判断是否有子分类
        if (productCategory1 != null) {
            //有子分类的不能删除
            return false;
        } else {
            ProductCategory productCategory2 = new ProductCategory();
            productCategory2.setId(id);
            return productCategoryTreeMapper.delete(productCategory2) > 0;
        }


    }


    private List<ProductCategoryTreeNodeVO> getProductCategoryTreeNodeVOS() {
        //查询所有的类别
        List<ProductCategory> productCategoryTrees = productCategoryTreeMapper.selectProductCategoryTree();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(productCategoryTrees)) {
            productCategoryTreeNodeVOS = productCategoryTrees.stream().map(
                    productCategory -> {
                        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = new ProductCategoryTreeNodeVO();
                        BeanUtils.copyProperties(productCategory, productCategoryTreeNodeVO);
                        return productCategoryTreeNodeVO;
                    }
            ).collect(Collectors.toList());
        }

        return productCategoryTreeNodeVOS;
    }
}








